<template>
  <div>
    <el-card>
      <!-- 面包屑导航 -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>网站管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-table :data="ListUser" style="width: 100%">
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="username" label="用户名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="capacity" label="可用容量"> </el-table-column>
        <el-table-column prop="key" label="Key"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-color="#409EFF"
              inactive-color="#ddd"
              @change="edit_state(scope.row)"
              :disabled="scope.row.username == 'admin' ? true : false"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="btnEdit btn" @click="edit_user(scope.row)">
              <i class="el-icon-edit"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="findInfo.pagenum"
        :page-sizes="[5, 10]"
        :page-size="findInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="findInfo.total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改 -->
    <el-dialog
      title="修改"
      :visible.sync="UserEditDialogVisible"
      width="50%"
      :before-close="UserEdithandleClose"
    >
      <el-form
        ref="EditUserForm"
        :rules="UserEditRules"
        :model="editForm"
        label-width="80px"
      >
        <el-form-item label="当前容量">
          <el-input v-model="editForm.capacity" disabled></el-input>
        </el-form-item>
        <el-form-item label="容量值" prop="num">
          <el-input v-model="editForm.num"></el-input>
          <span style="color:#f56c6c;"
            >注意：如果需要加值 +值 如 +10 减值 就是 -值 例如-10</span
          >
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="UserEditDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="OnsubmitEditUser">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      findInfo: {
        pagenum: 1,
        pagesize: 5,
        total: 0,
      },
      ListUser: [],
      UserEditDialogVisible: false,
      editForm: {},
      UserEditRules: {
        num: [{ required: true, message: "请输入容量值", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getListUser();
  },
  methods: {
    // 获取用户列表
    async getListUser() {
      const { data: res } = await this.$http.get("/users", {
        params: this.findInfo,
      });
      if (res.meta.status != 200) {
        this.$message.error(res.meta.msg);
        return;
      }
      this.ListUser = res.data.list;
      this.findInfo.total = res.data.total;
      console.log(this.ListUser);
    },
    // 修改用户状态
    async edit_state(row) {
      const { data: res } = await this.$http.put("/editste", {
        id: row.id,
        state: row.state,
      });
      if (res.meta.status != 200) {
        this.$message.error(res.meta.msg);
        row.state = !row.state
        return;
      }
      this.$message.success("更新用户状态成功！");
    },
    user_edit(row) {
      console.log(row);
    },
    UserEdithandleClose() {
      this.$refs.EditUserForm.resetFields();
      this.UserEditDialogVisible = false;
    },
    edit_user(row) {
      this.editForm = row;
      this.UserEditDialogVisible = true;
    },
    OnsubmitEditUser() {
      this.$refs.EditUserForm.validate(async (valid) => {
        if (!valid) {
          this.$message.error("请正确的填写表单！");
          return;
        }
        const { data: res } = await this.$http.put("/editcpt", {
          id: this.editForm.id,
          capacity: this.editForm.num,
        });
        if (res.meta.status != 200) {
          this.$message.error(res.meta.msg);
          return;
        }
        this.getListUser();
        this.$message.success("修改成功！");
        this.UserEditDialogVisible = false;
      });
    },
    handleSizeChange(size) {
      this.findInfo.pagesize = size;
      this.getListUser();
    },
    handleCurrentChange(page) {
      this.findInfo.pagenum = page;
      this.getListUser();
    },
  },
};
</script>

<style scoped>
.btn {
  margin-left: 5px;
  width: 24px;
  height: 22px;
  text-align: center;
  line-height: 22px;
  border-radius: 3px;
  cursor: pointer;
}
.btn i {
  color: #fff;
}
.btnEdit {
  background: #409eff;
}
</style>
